<template>
    <div class="demand-detail">
        <a-modal width="1300px" :visible="visible" title="编辑资源" @cancel="closeModal" class="yuan-modal demand-detail-modal"
            :dialog-style="{ top: '20px' }">
            <a-spin tip="Loading..." :spinning="saveLoading">
                <a-form-model layout="inline" class="yuan-form" ref="form" :model="formData" :rules="rules"
                    :class="isShow?'disable-form':''" :label-col="{ style: { width: '150px' } }"
                    :wrapper-col="{ style: { width: 'calc(100% - 160px)' } }">

                    <div class="form-panel">
                        <div class="form-panel-box">
                            <div class="form-title">banner信息</div>
                            <a-form-model-item label="封面图" class="yuan-form-item" v-if="pageReady" prop="pictures">
                                <UploadVue :uploadLength="9" @uploadChange="uploadChangeCoverImageUrl"
                                    :defImgList="formData.pictures.length>0?formData.pictures:[]" />
                                <div style="line-height:30px">jpg.png.gif格式.不超过2MB</div>
                                <div style="line-height:30px" class="c-danger" v-if="formData.pageTable">建议长宽比例为'4：1'</div>
                            </a-form-model-item>
                            <a-form-model-item class="yuan-form-item" label="banner标题" prop="pageTable">
                                <a-select style="width:200px" class="yuan-form-input"
                                    v-model="formData.pageTable" placeholder="请输入banner标题" :disabled="!!editInfo.id">
                                    <a-select-option value="首页banner">首页banner</a-select-option>
                                    <a-select-option value="创新需求banner">创新需求banner</a-select-option>
                                    <a-select-option value="成果直通车banner">成果直通车banner</a-select-option>
                                    <a-select-option value="成果公示banner">成果公示banner</a-select-option>
                                    <a-select-option value="资源对接banner">资源对接banner</a-select-option>
                                    <a-select-option value="人才库banner">人才库banner</a-select-option>
                                </a-select>
                            </a-form-model-item>
                        </div>
                    </div>
                </a-form-model>
            </a-spin>
            <template slot="footer">
                <template v-if="isShow">
                    <a-button @click="closeModal">
                        关闭
                    </a-button>
                </template>
                <template v-else>
                    <a-button @click="modalOk" type="primary" :loading="saveLoading">
                        确认保存
                    </a-button>
                    <a-button @click="closeModal">
                        取消
                    </a-button>
                </template>

            </template>
        </a-modal>
    </div>
</template>

<script lang="ts">
import { Component, Prop, Watch, Vue } from "vue-property-decorator";
import UploadVue from "@/components/upload.vue";
import api from "@/api/api";
@Component({
    name: "tpl",
    components: { UploadVue },
})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: false }) isShow!: boolean;
    @Prop({ default: "" }) editInfo!: any;

    @Watch("visible")
    visibleChange() {
        if (this.visible) {
            if (this.editInfo.id) {
                const pictures = this.editInfo.pictures ? JSON.parse(this.editInfo.pictures) : [];
                this.formData = { ...this.editInfo, pictures };
                console.info(this.formData);
                this.pageReady = true;
            } else {
                this.formData = {
                    pictures: [],
                };
                this.pageReady = true;
            }
        } else {
            this.pageReady = false;
        }
    }
    saveLoading = false;
    formData: any = {
        pictures: [],
    };
    rules = {
        pageTable: [{ required: true, message: "请输入banner标题", trigger: "change" }],
        pictures: [{ required: true, message: "请上传banner图片", trigger: "change" }],
    };
    pageReady = false;

    async mounted() {}
    closeModal() {
        this.$emit("closeModal");
    }

    async modalOk() {
        if (this.formData.pictures.length === 0) {
            this.$message.error("请上传封面图");
            return;
        }
        (this.$refs.form as any).validate(async (valid: boolean) => {
            if (!valid) {
                this.$message.error("必填信息不完整，请检查后重试");
                return;
            }
            this.saveLoading = true;
            try {
                const res = await api.addBanner(this.formData);
                this.$message.success("保存成功");
                this.closeModal();
                this.$emit("resetList");
            } finally {
                this.saveLoading = false;
            }
        });
    }
    uploadChangeCoverImageUrl(e) {
        this.formData.pictures = e.length ? e.map((item) => item.url) : [];
    }
}
</script>
<style lang="scss">
.demand-detail-modal {
}
</style>
